app.controller('openstackPerformanceCtrl', ['$rootScope', '$scope', 'httpLoad','$interval','$state', function($rootScope, $scope, httpLoad, $interval, $state) {
    $rootScope.moduleTitle = '平台管理 > OpenStack > 性能数据';
    $rootScope.link = '/statics/css/graph.css';
    var params = JSON.parse(sessionStorage.getItem('performance'));
    var url = '/vhost/performance';
    if(params.flag == 2) url = '/vm/performance';
    $scope.timeTypeData  = [
        {name:'实时',value:'REAL'},
        {name:'一天',value:'DAY'},
        {name:'一周',value:'WEEK'},
        {name:'一月',value:'MONTH'}
    ];
    $scope.options = {
        CPU:{
            unit:'单位/百分比'
        },
        MEM:{
            unit:'单位/MB'
        },
        DISK:{
            unit:'单位/GB'
        }
    };
    $scope.typeData  = [
        {name:'CPU',value:'CPU'},
        {name:'内存',value:'MEM'},
        {name:'磁盘',value:'DISK'}
    ];
    $scope.time = 'REAL';
    $scope.type = 'CPU';
    //获取性能数据
    $scope.getPerformance = function () {
        $scope.options.type = $scope.type;
        var paramObj = {
            type:$scope.type,
            time:$scope.time,
            name:params.name,
            vendorId:params.vendorId
        };
        httpLoad.loadData({
            url: url,
            method: 'GET',
            data:paramObj,
            success: function(data){
                if(data.success) {
                    data.data.param = paramObj;
                    $scope.performance = data.data;
                }else if($scope.timeInterval){
                    $interval.cancel($scope.timeInterval);
                    $scope.timeInterval = '';
                }
            }
        });
    };
    $scope.getPerformance();
    var setTimeFun = function () {
        $scope.timeInterval = $interval(function () {
            $scope.getPerformance ();
        }, 20*1000);
    };
    $scope.selectTimeType = function () {
        if($scope.time == 'REAL') setTimeFun();
        else if($scope.timeInterval){
            $interval.cancel($scope.timeInterval);
            $scope.timeInterval = '';
        }
        $scope.getPerformance();
    };
    $scope.selectTimeType();
    $scope.$on("$destroy", function(event) {
        $interval.cancel($scope.timeInterval);
    });
    $scope.goBack = function(){
        history.go(-1)
    };
}]);
app.directive('ngPerformanceLineChart2', ['$timeout', function ($timeout) {
    return {
        restrict: 'EA',
        scope:{
            data:'=',
            options:'='
        },
        link: function (scope, element, attrs) {
            var chart = echarts.init($(element)[0]);
            var initEchart = function (xAxis,legend,series,isClear) {
                var option = {
                    backgroundColor:'#fff',
                    title:{
                        text:scope.options.theme,
                        textStyle:{
                            fontWeight: 'bold',
                            fontSize: 16,
                            color:'#333'
                        }
                    },
                    tooltip : {
                        trigger: 'axis'
                    },
                    legend: {
                        data:legend,
                        orient : 'horizontal',
                        top:'1%',
                    },
                    toolbox: {
                        show : true,
                        feature : {
                            mark : {show: true},
                            magicType : {show: true, type: ['line', 'stack', 'tiled','bar']},
                            saveAsImage : {show: true}
                        }
                    },
                    calculable : true,
                    xAxis : [
                        {
                            type : 'category',
                            boundaryGap: false,
                            data : xAxis
                        }
                    ],
                    yAxis : [
                        {
                            name:scope.options[scope.options.type].unit,
                            type : 'value'
                        }
                    ],
                    grid: {
                        left: '1%',
                        right: '20',
                        top:'10%',
                        bottom: '6%',
                        containLabel: true
                    },
                    series : series
                };
                if(isClear) chart.clear();
                chart.setOption(option);
            };
            scope.$watch('data',function (newValue,oldValue) {
                var legendData = [],series = [];
                scope.data.values.forEach(function (item) {
                    legendData.push(item.name);
                    series.push({
                        name:item.name,
                        type:'line',
                        smooth:true,
                        itemStyle: {
                            normal: {
                                // areaStyle: {type: 'default'},
                                opacity:0.98
                            }},
                        data:item.data
                    });
                });
                var isClear = true;
                if(newValue.param.type==oldValue.param.type&&newValue.param.time==oldValue.param.time){
                    isClear = false;
                }
                initEchart(scope.data.keys,legendData,series,isClear);
            });
        }}
}]);